package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.*;
import com.qf.pojo.*;
import com.qf.service.DtsGoodsAttributeService;
import com.qf.service.DtsGoodsProductService;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsGoodsSpecificationService;
import com.qf.util.AdminResponseUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.GoodsAllinone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {
    @Autowired
    DtsGoodsMapper dtsGoodsMapper;
    @Override
    public Integer getTotal() {
        Integer integer = dtsGoodsMapper.selectCount(null);
        return integer;
    }

    @Override
    public IPage<DtsGoods> findPage(Integer page, Integer limit, String sort, String order, String goodsSn, String name, List<Integer> brandsId) {
        IPage<DtsGoods> ipage = new Page<>(page,limit);
        //创建条件
        QueryWrapper<DtsGoods> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if(order.equalsIgnoreCase("desc")) {
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }

        if(!StringUtils.isEmpty(goodsSn)) {
            qw.like("goods_sn", goodsSn);
        }

        if(!StringUtils.isEmpty(name)) {
            qw.like("name", name);
        }

        if(!CollectionUtils.isEmpty(brandsId)){
            qw.in("brand_id", brandsId);
        }
        dtsGoodsMapper.selectPage(ipage,qw);
        return ipage;
    }
    @Autowired
    DtsGoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    DtsGoodsProductMapper goodsProductMapper;

    @Autowired
    DtsGoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    DtsCategoryMapper categoryMapper;
    @Override
    public Map<String, Object> findByDetail(Integer id) {
        DtsGoods dtsGoods = dtsGoodsMapper.selectById(id);
        //商品的属性信息
        LambdaQueryWrapper<DtsGoodsAttribute> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(DtsGoodsAttribute::getGoodsId,id);
        qw1.eq(DtsGoodsAttribute::getDeleted,false);
        List<DtsGoodsAttribute> attributes = goodsAttributeMapper.selectList(qw1);
        //商品的库存
        LambdaQueryWrapper<DtsGoodsProduct> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(DtsGoodsProduct::getId,id);
        qw2.eq(DtsGoodsProduct::getDeleted,false);
        List<DtsGoodsProduct> dtsGoodsProducts = goodsProductMapper.selectList(qw2);

        LambdaQueryWrapper<DtsGoodsSpecification> qw3 = new LambdaQueryWrapper<>();
        qw3.eq(DtsGoodsSpecification::getGoodsId , id);
        qw3.eq(DtsGoodsSpecification::getDeleted, false); //只查询没有被逻辑删除的属性

        List<DtsGoodsSpecification> specifications = goodsSpecificationMapper.selectList(qw3);
        //查询商品的分类
        Integer categoryId = dtsGoods.getCategoryId();//二级分类
        DtsCategory dtsCategory = categoryMapper.selectById(categoryId);

        Integer pid = dtsCategory.getPid();//一级目录的分类
        Map<String,Object> data = new HashMap<>();
        data.put("categoryIds", Arrays.asList(pid,categoryId));
        data.put("goods",dtsGoods);
        data.put("attributes",attributes);
        data.put("specifications",specifications);
        data.put("products",dtsGoodsProducts);


        return data;
    }

    @Autowired
    DtsBrandMapper brandMapper;

    private Object checkParam(GoodsAllinone goodsAllinone) {
        //获取商品信息
        DtsGoods goods = goodsAllinone.getGoods();
        if(goods == null){
            return ResponseUtil.badArgument();
        }

        if(StringUtils.isEmpty(goods.getName())) {
            return AdminResponseUtil.fail(-1, "商品名称不能为空");
        }

        if(StringUtils.isEmpty(goods.getGoodsSn())) {
            return AdminResponseUtil.fail(-1, "商品编号不能为空");
        }
        //品牌id
        Integer brandId = goods.getBrandId();
        //判断id是否存在
        DtsBrand brand = brandMapper.selectById(brandId);
        if(brand == null) {
            return AdminResponseUtil.fail(-1, "品牌商不存在");
        }
        Integer categoryId = goods.getCategoryId();
        DtsCategory category = categoryMapper.selectById(categoryId);
        if(category == null) {
            return AdminResponseUtil.fail(-1, "分类不存在");
        }
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        if(attributes == null && attributes.length == 0) {
            return AdminResponseUtil.fail(-1, "商品属性不能为空");
        }

        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        if(products == null && products.length == 0) {
            return AdminResponseUtil.fail(-1, "商品库存不能为空");
        }
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        if(specifications == null && specifications.length == 0) {
            return AdminResponseUtil.fail(-1, "商品规格不能为空");
        }
        return null;
    }

    @Autowired
    DtsGoodsProductService goodsProductService;
    @Autowired
    DtsGoodsSpecificationService specificationService;

    @Autowired
    DtsGoodsAttributeService attributeService;
    @Override
    public Object update(GoodsAllinone goodsAllinone) {
        Object o = this.checkParam(goodsAllinone);
        if (o != null){
            return o;
        }
        //更新商品
        DtsGoods goods = goodsAllinone.getGoods();
        goods.setUpdateTime(new Date());
        dtsGoodsMapper.updateById(goods);
        //更新库存表
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        goodsProductService.updataProducts(goods.getId(),products);
        //更新规格表
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        specificationService.updateSpecifications(goods.getId(),specifications);
        //更新属性表
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        attributeService.updateAttributes(goods.getId(), attributes);
        return ResponseUtil.ok();
    }


}
